Declare @InitialStartDate datetime
SET @InitialStartDate = '02/Oct/2010'
--select datename(dw, @InitialStartDate)
Declare @iDay int
Declare @iWeek int
Declare @strWeek varchar(10)
Declare @iCount int

SET @iDay = 6
SET @strWeek = 'Week '
SET @iWeek = 1
SET @iCount = 0
WHILE @InitialStartDate < (GETDATE()-1) 
BEGIN
	--Select @strWeek + CAST(@iWeek as varchar) WeekCount, @InitialStartDate DateTimeOfQuote, 
	--datename(dw, @InitialStartDate) QuoteDayName
	--INSERT INTO tblTempCarQuoteCount( WeekCount, QuoteDate, QuoteDayName, QuoteCount)
	Select @strWeek + CAST(@iWeek as varchar) WeekCount, @InitialStartDate DateTimeOfQuote, 
	datename(dw, @InitialStartDate) QuoteDayName,  COUNT(CECarId) QuoteCount from tblCarPolicy 
	Where Convert(varchar(8), CreatedDate ,112) =  Convert(varchar(8), @InitialStartDate ,112)
	AND RefCEUserId NOT IN 
	(Select RefCEUserId from tblCarPolicy Where CreatedDate < @InitialStartDate)
	
	SET @iDay = @iDay + 1
	
	IF @iDay = 8
		BEGIN
			SET @iDay = 1
			SET @iWeek = @iWeek + 1
		END
	SET @InitialStartDate = @InitialStartDate + 1
END


	Select Convert(varchar(12), cd.CreatedDate, 106), (Select COUNT(CECarId) QuoteCount from tblCarDetail 
	Where Convert(varchar(12), CreatedDate ,106) =  Convert(varchar(12), cd.CreatedDate ,106)
	And RefCEUserId IN 
	(Select CEID from tblCEUser Where Convert(varchar(12), CreatedDate ,106) =  Convert(varchar(12), cd.CreatedDate ,106))) TotalNewUserQuote,
	(Select COUNT(CECarId) QuoteCount from tblCarDetail 
	Where Convert(varchar(12), ModifiedDate ,106) =  Convert(varchar(12), cd.CreatedDate ,106)) TotalAllUserQuote From tblCarDetail cd
	WHERE Convert(varchar(6), cd.CreatedDate ,112) =  '201104'
	Group By Convert(varchar(12), cd.CreatedDate, 106)
	
	
	
	